<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="curId ? '编辑角色' : '新增角色'"
    width="700px"
    :before-close="close"
  >
    <el-form
      ref="ruleForm"
      label-position="left"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="角色" prop="name">
        <el-input v-model="ruleForm.name" clearable autocomplete="off" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="ruleForm.desc" clearable autocomplete="off" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getRole, updateRole, createRole } from "@/api/user/roles";
// import { getRoles } from "@/api/user/roles";

export default {
  name: "RolesCuForm",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    curId: {
      type: Number,
      default: null,
    },
  },
  data() {
    return {
      ruleForm: {
        name: "",
        desc: "",
      },
      rolesData: [],
      rules: {
        name: [{ required: true, trigger: "blur", message: "角色名不能为空" }],
      },
    };
  },
  watch: {
    dialogVisible(v) {
      console.log("监听到子组件开启");
      if (v) {
        if (this.curId) {
          console.log("传入cuId:", this.curId);
          getRole(this.curId).then((response) => {
            this.ruleForm = response.data;
            console.log("getDate:", response.data); // 将'response'替换为'res'
          });
        }
      }
    },
  },
  methods: {
    close() {
      this.$refs.ruleForm.resetFields();
      this.$emit("close");
    },
    search() {
      this.close();
      this.$emit("search");
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.curId) {
            updateRole(this.curId, this.ruleForm).then((res) => {
              this.$message({
                message: "修改成功",
                type: "success",
              });
              this.search();
            });
          } else {
            createRole(this.ruleForm).then((res) => {
              this.$message({
                message: "新增成功",
                type: "success",
              });
              this.search();
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>